Completed
Push — master ( e65836...fa4d5c )
by Mathieu
12s queued 10s
created

UpdateCustomerAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 19 2
1
import {
2
  Body,
3
  Controller,
4
  Inject,
5
  BadRequestException,
6
  UseGuards,
7
  Put,
8
  Param
9
} from '@nestjs/common';
10
import {AuthGuard} from '@nestjs/passport';
11
import {ApiTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
12
import {ICommandBus} from 'src/Application/ICommandBus';
13
import {UpdateCustomerCommand} from 'src/Application/Customer/Command/UpdateCustomerCommand';
14
import {CustomerDTO} from '../DTO/CustomerDTO';
15
import {IdDTO} from 'src/Infrastructure/Common/DTO/IdDTO';
16
import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
17
import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
18
import {UserRole} from 'src/Domain/HumanResource/User/User.entity';
19
20
@Controller('customers')
21
@ApiTags('Customer')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'), RolesGuard)
24
export class UpdateCustomerAction {
25
  constructor(
26
    @Inject('ICommandBus')
27
    private readonly commandBus: ICommandBus
28
  ) {}
29
30
  @Put(':id')
31
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
32
  @ApiOperation({summary: 'Update customer'})
33
  public async index(@Param() dto: IdDTO, @Body() customerDto: CustomerDTO) {
34
    try {
35
      const {
36
        address: {street, city, zipCode, country},
37
        name
38
      } = customerDto;
39
      const {id} = dto;
40
41
      await this.commandBus.execute(
42
        new UpdateCustomerCommand(id, name, street, city, zipCode, country)
43
      );
44
45
      return {id};
46
    } catch (e) {
47
      throw new BadRequestException(e.message);
48
    }
49
  }
50
}
51